from application import db


class Users(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(128), nullable=False)
    password = db.Column(db.String(256), nullable=False)
    mobile = db.Column(db.String(32), nullable=False, unique=True)
    # 用户类型(1：参数人员，2：评委，3：管理人员)
    user_type = db.Column(db.Integer, nullable=False)
    status = db.Column(db.SmallInteger, default=1)
    # 是否有参赛资格（1：有，0：没有）
    qualification = db.Column(db.SmallInteger, default=1)
    # 是否能给作品打分(1:可以， 0：不可以)
    jurisdiction = db.Column(db.SmallInteger, default=0)

    work = db.relationship('WorkDetail', backref='users', uselist=False)
    rotation = db.relationship('Rotation', backref='users', uselist=False)

    def __repr__(self):
        return self.username


class WorkDetail(db.Model):
    __tablename__ = 'work_detail'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False)
    upload_time = db.Column(db.DateTime, nullable=True)
    score = db.Column(db.Integer, nullable=True)
    rating_time = db.Column(db.DateTime, nullable=True)
    rater = db.Column(db.String(128), nullable=True)

    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))

    def __repr__(self):
        return self.id


class Rotation(db.Model):
    __tablename__ = 'rotation'
    id = db.Column(db.Integer, primary_key=True)
    first_round = db.Column(db.Integer, nullable=True)
    second_round = db.Column(db.Integer, nullable=True)
    final_round = db.Column(db.Integer, nullable=True)

    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))

    def __repr__(self):
        self.id


